<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="主表ID" prop="mainId">
        <el-input v-model="formData.mainId" placeholder="请输入主表ID" />
      </el-form-item>
      <el-form-item label="交易单元ID" prop="tradingUnitId">
        <el-input v-model="formData.tradingUnitId" placeholder="请输入交易单元ID" />
      </el-form-item>
      <el-form-item label="转让日期开始时间" prop="transferDateStart">
        <el-input v-model="formData.transferDateStart" placeholder="请输入转让日期开始时间" />
      </el-form-item>
      <el-form-item label="转让日期结束时间" prop="transferDateEnd">
        <el-input v-model="formData.transferDateEnd" placeholder="请输入转让日期结束时间" />
      </el-form-item>
      <el-form-item label="转让类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择转让类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="参与交易单元" prop="participateTradingId">
        <el-input v-model="formData.participateTradingId" placeholder="请输入参与交易单元" />
      </el-form-item>
      <el-form-item label="我方不参与" prop="notTrading">
        <el-input v-model="formData.notTrading" placeholder="请输入我方不参与" />
      </el-form-item>
      <el-form-item label="预计操作日期" prop="expectedDate">
        <el-date-picker
          v-model="formData.expectedDate"
          type="date"
          value-format="x"
          placeholder="选择预计操作日期"
        />
      </el-form-item>
      <el-form-item label="实际操作金额" prop="actualAmount">
        <el-input v-model="formData.actualAmount" placeholder="请输入实际操作金额" />
      </el-form-item>
      <el-form-item label="转让方操作截图" prop="transferorImg">
        <el-input v-model="formData.transferorImg" placeholder="请输入转让方操作截图" />
      </el-form-item>
      <el-form-item label="受让方操作截图" prop="transfereeImg">
        <el-input v-model="formData.transfereeImg" placeholder="请输入受让方操作截图" />
      </el-form-item>
      <el-form-item label="付款截图" prop="paymentImg">
        <el-input v-model="formData.paymentImg" placeholder="请输入付款截图" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DemandInfoTradingUnitApi, DemandInfoTradingUnitVO } from '@/api/dd/demandinfotradingunit'

/** 需求信息-交易单元 表单 */
defineOptions({ name: 'DemandInfoTradingUnitForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  mainId: undefined,
  tradingUnitId: undefined,
  transferDateStart: undefined,
  transferDateEnd: undefined,
  type: undefined,
  participateTradingId: undefined,
  notTrading: undefined,
  expectedDate: undefined,
  actualAmount: undefined,
  transferorImg: undefined,
  transfereeImg: undefined,
  paymentImg: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DemandInfoTradingUnitApi.getDemandInfoTradingUnit(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DemandInfoTradingUnitVO
    if (formType.value === 'create') {
      await DemandInfoTradingUnitApi.createDemandInfoTradingUnit(data)
      message.success(t('common.createSuccess'))
    } else {
      await DemandInfoTradingUnitApi.updateDemandInfoTradingUnit(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    mainId: undefined,
    tradingUnitId: undefined,
    transferDateStart: undefined,
    transferDateEnd: undefined,
    type: undefined,
    participateTradingId: undefined,
    notTrading: undefined,
    expectedDate: undefined,
    actualAmount: undefined,
    transferorImg: undefined,
    transfereeImg: undefined,
    paymentImg: undefined,
  }
  formRef.value?.resetFields()
}
</script>